<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

/**
 * 管理员操作记录
 * @param $log_url 操作URL
 * @param $log_info 记录信息
 */
function adminLog($log_info) {
    $add['log_time'] = time();
    $add['admin_id'] = session('admin_id');
    $add['log_info'] = $log_info;
    $add['log_ip'] = getIP();
    $add['log_url'] = __ACTION__;
    M('admin_log')->add($add);
}

function getAdminInfo($admin_id) {
    return D('admin')->where("admin_id=$admin_id")->find();
}

function tpversion() {
    if (!empty($_SESSION['isset_push']))
        return false;
    $_SESSION['isset_push'] = 1;
    error_reporting(0); //关闭所有错误报告
    $app_path = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
    $version_txt_path = $app_path . '/Application/Admin/Conf/version.txt';
    $curent_version = file_get_contents($version_txt_path);

    $vaules = array(
        'domain' => $_SERVER['HTTP_HOST'],
        'last_domain' => $_SERVER['HTTP_HOST'],
        'key_num' => $curent_version,
        'install_time' => INSTALL_DATE,
        'cpu' => '0001',
        'mac' => '0002',
        'serial_number' => SERIALNUMBER,
    );
    $url = "http://service.xiarikui.com/index.php?m=Home&c=Index&a=user_push&" . http_build_query($vaules);
    stream_context_set_default(array('http' => array('timeout' => 3)));
    file_get_contents($url);
}

/**
 * 面包屑导航  用于后台管理
 * 根据当前的控制器名称 和 action 方法
 */
function navigate_admin() {
    $navigate = include APP_PATH . 'Common/Conf/navigate.php';
    $location = strtolower('Admin/' . CONTROLLER_NAME);
    $arr = array(
        $navigate[$location]['name'] => 'javascript:;',
        $navigate[$location]['action'][ACTION_NAME] => 'javascript:;',
    );
    return $arr;
}

/**
 * 导出excel
 * @param $strTable	表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable, $filename) {
    header("Content-type: application/vnd.ms-excel");
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=" . $filename . "_" . date('Y-m-d') . ".xls");
    header('Expires:0');
    header('Pragma:public');
    echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . $strTable . '</html>';
}

/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '') {
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++)
        $size /= 1024;
    return round($size, 2) . $delimiter . $units[$i];
}

/**
 * 根据id获取地区名字
 * @param $regionId id
 */
function getRegionName($regionId) {
    $data = M('region')->where(array('id' => $regionId))->field('name')->find();
    return $data['name'];
}

function getMenuList($act_list) {
    //根据角色权限过滤菜单
    $menu_list = getAllMenu();
    if ($act_list != 'all') {
        $right = M('sys_menu')->where("id in ($act_list)")->getField('right', true);
        foreach ($right as $val) {
            $role_right .= $val . ',';
        }
        $role_right = explode(',', $role_right);
        foreach ($menu_list as $k => $mrr) {
            foreach ($mrr['sub_menu'] as $j => $v) {
                if (isset($v['sub_menu'])) {
                    foreach ($v['sub_menu'] as $kk => $vv) {
                        if (!in_array($vv['control'] . 'Controller@' . $vv['act'], $role_right)) {
                            unset($menu_list[$k]['sub_menu'][$j]); //过滤菜单
                        }
                    }
                } else {
                    if (!in_array($v['control'] . 'Controller@' . $v['act'], $role_right)) {
                        unset($menu_list[$k]['sub_menu'][$j]); //过滤菜单
                    }
                }
            }
        }
    }
    return $menu_list;
}

/**
 * 获取所有菜单
 * @return array
 */
function getAllMenu() {
    $menu = array(
        'system' => array('name' => '系统设置', 'icon' => 'fa-cog', 'sub_menu' => array(
                array('name' => '网站设置', 'act' => 'index', 'control' => 'System'),
                //array('name' => '友情链接', 'act' => 'linkList', 'control' => 'Article'),
                //array('name' => '自定义导航', 'act' => 'navigationList', 'control' => 'System'),
                array('name' => '区域管理', 'act' => 'region', 'control' => 'Tools'),
                //array('name' => '权限资源列表', 'act' => 'rightList', 'control' => 'System'),
            )),
        'access' => array('name' => '权限管理', 'icon' => 'fa-gears', 'sub_menu' => array(
                array('name' => '管理员列表', 'act' => 'index', 'control' => 'Admin'),
                array('name' => '角色管理', 'act' => 'role', 'control' => 'Admin'),
                //array('name' => '供应商管理', 'act' => 'supplier', 'control' => 'Admin'),
                array('name' => '管理员日志', 'act' => 'log', 'control' => 'Admin'),
            )),
        'push' => array('name' => '推送管理', 'icon' => 'fa-flag', 'sub_menu' => array(
            array('name' => '推送设置', 'act' => 'setting', 'control' => 'PushMsg'),
            array('name' => '推送管理', 'act' => 'index', 'control' => 'PushMsg'),
        )),

        'count' => array('name' => '统计管理', 'icon' => 'fa-cubes', 'sub_menu' => array(
            array('name' => '问题统计', 'act' => 'index', 'control' => 'Count'),
            array('name' => '搜索统计', 'act' => 'search', 'control' => 'Count'),
        )),

        'finance' => array('name' => '财务管理', 'icon' => 'fa-signal', 'sub_menu' => array(
            array('name' => '赏金提现', 'act' => 'log', 'control' => 'Finance'),
            array('name' => '赏金明细', 'act' => 'detail', 'control' => 'Finance'),
            array('name' => '交易记录', 'act' => 'trade', 'control' => 'Finance'),
        )),

//        'module' => array('name' => '模块管理', 'icon' => 'fa-gears', 'sub_menu' => array(
//                array('name' => '菜单管理', 'act' => 'index2', 'control' => 'Admin'),
//                array('name' => '模块列表', 'act' => 'role2', 'control' => 'Admin'),
//            )),
        'member' => array('name' => '会员管理', 'icon' => 'fa-user', 'sub_menu' => array(
                array('name' => '会员列表', 'act' => 'index', 'control' => 'User'),
                //array('name' => '会员等级', 'act' => 'levelList', 'control' => 'User'),
                array('name' => '支付记录', 'act' => 'log', 'control' => 'User'),
               // array('name' => '余额提现', 'act' => 'account', 'control' => 'User'),
//                array('name' => '会员整合', 'act' => 'integrate', 'control' => 'User'),
            )),

        'banner' => array('name' => '轮播图管理', 'icon' => 'fa-book', 'sub_menu' => array(
            array('name' => '轮播图列表', 'act' => 'index', 'control' => 'banner'),
        )),

        'cat' => array('name' => '分类管理', 'icon' => 'fa-book', 'sub_menu' => array(
            array('name' => '分类列表', 'act' => 'cat', 'control' => 'Cat'),
        )),

        'qa' => array('name' => '问答管理', 'icon' => 'fa-book', 'sub_menu' => array(
            array('name' => '问答列表', 'act' => 'index', 'control' => 'Qa'),
        )),

        'accountant' => array('name' => '会计师管理', 'icon' => 'fa-user', 'sub_menu' => array(
            array('name' => '会计师列表', 'act' => 'index', 'control' => 'Accountant'),
        )),

//        'goods' => array('name' => '商品管理', 'icon' => 'fa-book', 'sub_menu' => array(
//                array('name' => '商品分类', 'act' => 'categoryList', 'control' => 'Goods'),
//                array('name' => '商品列表', 'act' => 'goodsList', 'control' => 'Goods'),
//                array('name' => '商品类型', 'act' => 'goodsTypeList', 'control' => 'Goods'),
//                array('name' => '商品规格', 'act' => 'specList', 'control' => 'Goods'),
//                array('name' => '商品属性', 'act' => 'goodsAttrList', 'control' => 'Goods'),
//                array('name' => '品牌列表', 'act' => 'brandList', 'control' => 'Goods'),
//                array('name' => '商品评论', 'act' => 'index', 'control' => 'Comment'),
//                array('name' => '商品咨询', 'act' => 'askList', 'control' => 'Comment'),
//            )),
//        'order' => array('name' => '订单管理', 'icon' => 'fa-money', 'sub_menu' => array(
//                array('name' => '订单列表', 'act' => 'index', 'control' => 'Order'),
//                array('name' => '发货单', 'act' => 'deliveryList', 'control' => 'Order'),
////                array('name' => '快递单', 'act'=>'express_list', 'control'=>'Order'),
//                array('name' => '退货单', 'act' => 'returnList', 'control' => 'Order'),
//                array('name' => '添加订单', 'act' => 'addOrder', 'control' => 'Order'),
//                array('name' => '订单日志', 'act' => 'orderLog', 'control' => 'Order'),
//            )),
//        'promotion' => array('name' => '促销管理', 'icon' => 'fa-bell', 'sub_menu' => array(
//                array('name' => '拼团管理', 'act' => 'fightGroupList', 'control' => 'Promotion'),
//                array('name' => '抢购管理', 'act' => 'limitBuy', 'control' => 'Promotion'),
//                array('name' => '团购管理', 'act' => 'goodsGrouponList', 'control' => 'Promotion'),
//                array('name' => '商品促销', 'act' => 'actionGoodsList', 'control' => 'Promotion'),
//                array('name' => '订单促销', 'act' => 'actionOrderList', 'control' => 'Promotion'),
//                array('name' => '优惠券管理', 'act' => 'index', 'control' => 'Coupon'),
//            )),
//        'Ad' => array('name' => '广告管理', 'icon' => 'fa-flag', 'sub_menu' => array(
//                array('name' => '广告列表', 'act' => 'adList', 'control' => 'Ad'),
//                array('name' => '广告位置', 'act' => 'positionList', 'control' => 'Ad'),
//            )),
//        'content' => array('name' => '内容管理', 'icon' => 'fa-comments', 'sub_menu' => array(
//                array('name' => '文章列表', 'act' => 'articleList', 'control' => 'Article'),
//                array('name' => '文章分类', 'act' => 'categoryList', 'control' => 'Article'),
////                array('name' => '帮助管理', 'act'=>'helpList', 'control'=>'Article'),
////                array('name' => '公告管理', 'act'=>'noticeList', 'control'=>'Article'),
////                array('name' => '专题列表', 'act' => 'specialList', 'control' => 'Special'),
//            )),
//        'weixin' => array('name' => '微信管理', 'icon' => 'fa-weixin', 'sub_menu' => array(
//                array('name' => '公众号管理', 'act' => 'index', 'control' => 'Wechat'),
//                array('name' => '微信菜单管理', 'act' => 'menu', 'control' => 'Wechat'),
//                array('name' => '文本回复', 'act' => 'text', 'control' => 'Wechat'),
//                array('name' => '图文回复', 'act' => 'img', 'control' => 'Wechat'),
//                array('name' => '消息推送', 'act' => 'news', 'control' => 'Wechat'),
//                array('name' => '组合回复', 'act' => 'nes', 'control' => 'Wechat'),
////                array('name' => '抽奖活动', 'act' => 'nes', 'control' => 'Wechat'),
//            )),
//        'application' => array('name' => '应用中心', 'icon' => 'fa-adjust', 'sub_menu' => array(
//                array('name' => '应用列表', 'act' => 'index', 'control' => 'Application')
//            )),
//        'distribut' => array('name' => '分销管理', 'icon' => 'fa-cubes', 'sub_menu' => array(
//                array('name' => '分销关系', 'act' => 'index', 'control' => 'AppDistribut'),
//                array('name' => '分销设置', 'act' => 'set', 'control' => 'AppDistribut'),
////                array('name' => '申请列表', 'act' => 'apply', 'control' => 'AppDistribut'),
//                array('name' => '提现列表', 'act' => 'cash', 'control' => 'AppDistribut'),
//                array('name' => '分成列表', 'act' => 'bonus', 'control' => 'AppDistribut'),
//                array('name' => '汇款列表', 'act' => 'record', 'control' => 'AppDistribut'),
//            )),
//        'theme' => array('name' => '模板管理', 'icon' => 'fa-adjust', 'sub_menu' => array(
//                array('name' => 'PC端模板', 'act' => 'templateList?t=pc', 'control' => 'Template'),
//                array('name' => '手机端模板', 'act' => 'templateList?t=mobile', 'control' => 'Template'),
//            )),
//        'tools' => array('name' => '插件工具', 'icon' => 'fa-plug', 'sub_menu' => array(
//                array('name' => '插件列表', 'act' => 'index', 'control' => 'Plugin'),
////                array('name' => '数据备份', 'act' => 'index', 'control' => 'Tools'),
////                array('name' => '数据还原', 'act' => 'restore', 'control' => 'Tools'),
//            )),
//        'count' => array('name' => '统计报表', 'icon' => 'fa-signal', 'sub_menu' => array(
////                array('name' => '销售概况', 'act' => 'index', 'control' => 'Report'),
//                array('name' => '销售排行', 'act' => 'saleTop', 'control' => 'Report'),
//                array('name' => '会员排行', 'act' => 'userTop', 'control' => 'Report'),
//                array('name' => '销售明细', 'act' => 'saleList', 'control' => 'Report'),
//                array('name' => '会员统计', 'act' => 'user', 'control' => 'Report'),
//                array('name' => '财务统计', 'act' => 'finance', 'control' => 'Report'),
//            )),
//        'pick' => array('name' => '自提点管理', 'icon' => 'fa-anchor', 'sub_menu' => array(
//                array('name' => '自提点列表', 'act' => 'index', 'control' => 'Pick'),
//                array('name' => '添加自提点', 'act' => 'add', 'control' => 'Pick'),
//            ))

        'tools' => array('name' => '插件工具', 'icon' => 'fa-plug', 'sub_menu' => array(
                array('name' => '插件列表', 'act' => 'index', 'control' => 'Plugin'),
              // array('name' => '数据备份', 'act' => 'index', 'control' => 'Tools'),
               //array('name' => '数据还原', 'act' => 'restore', 'control' => 'Tools'),
            )),

    );
//    $menu = file_get_contents(APP_PATH . 'Admin/Conf/menu.txt');
//    $result = json_decode($menu, true);
//      dump($result);
    return $menu;
}

function respose($res) {
    exit(json_encode($res));
}
